package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SaleListGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 */
@Service
@Slf4j
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    LogService logService;

    @Override
    public ServiceVO getCode() {

        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for(int i = 4;i > intCode.toString().length();i--){

            unitCode = "0"+unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }

    /**
     * @description 商品信息Controller
     */
    @Override
    public Map<String, Object> listIndexStock(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        Map<String,Object> map = new HashMap<>();

        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;

        List<Goods> list = goodsDao.listIndexStockByCodeOrNameOrType(offSet,rows,codeOrName,goodsTypeId);

        logService.save(new Log(Log.SELECT_ACTION,"分页查询库存信息"));

        map.put("total", goodsDao.getGoodsCount(codeOrName));
        map.put("rows", list);

        return map;

    }

    //分页查询商品信息
    @Override
    public Map<String, Object> getPageGoodsList(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        Map<String,Object> map = new HashMap<>();

        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;

        List<Goods> list = goodsDao.getPageGoodsList(offSet,rows,goodsName,goodsTypeId);

        logService.save(new Log(Log.SELECT_ACTION,"分页查询商品信息"));

        map.put("total", goodsDao.getPageGoodsCount(goodsName));
        map.put("rows", list);

        return map;
    }

    //添加或修改商品信息
    @Override
    public ServiceVO saveGoods(Goods goods) {
// 商品ID为空时，说明是新增操作，需要先判断供应商名称是否存在
        if(goods.getGoodsId() == null){

            Goods goodsByName = goodsDao.findGoodsByName(goods.getGoodsName());

            if (goodsByName != null) {
                return new ServiceVO(ErrorCode.GOODS_EXIST_CODE, ErrorCode.GOODS_EXIST_MESS);
            }

            goodsDao.saveGoods(goods);
            logService.save(new Log(Log.INSERT_ACTION,"添加商品:"+goods.getGoodsName()));

        } else {

            goodsDao.updateGoods(goods);
            logService.save(new Log(Log.UPDATE_ACTION,"修改商品:"+goods.getGoodsName()));

        }

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);

    }

    //删除商品信息
    @Override
    public ServiceVO deleteGoods(Integer goodsId) {
        logService.save(new Log(Log.DELETE_ACTION,"删除商品:"+goodsId));

        goodsDao.deleteGoods(goodsId);

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    //分页查询无库存商品信息
    @Override
    public Map<String, Object> getPageNoInQuGoods(Integer page, Integer rows, String nameOrCode) {
        Map<String,Object> map = new HashMap<>();

        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;

        List<Goods> list = goodsDao.getPageNoInQuGoods(offSet,rows,nameOrCode);

        logService.save(new Log(Log.SELECT_ACTION,"分页查询无库存商品信息"));

        map.put("total", goodsDao.getPageNoInQuCount(nameOrCode));
        map.put("rows", list);

        return map;

    }

    //分页查询有库存商品信息
    @Override
    public Map<String, Object> getPageHasInQuGoods(Integer page, Integer rows, String nameOrCode) {
        Map<String,Object> map = new HashMap<>();

        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;

        List<Goods> list = goodsDao.getPageHasInQuGoods(offSet,rows,nameOrCode);

        logService.save(new Log(Log.SELECT_ACTION,"分页查询有库存商品信息"));

        map.put("total", goodsDao.getPageHasInQuCount(nameOrCode));
        map.put("rows", list);

        return map;
    }

    //添加商品期初库存
    @Override
    public ServiceVO saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        if (!StringUtils.isEmpty(goodsId)){
            goodsDao.saveStock(goodsId,inventoryQuantity,purchasingPrice);
            logService.save(new Log(Log.UPDATE_ACTION,"添加商品期初库存:"+goodsId));
        }

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    //删除商品库存
    @Override
    public ServiceVO deleteStock(Integer goodsId) {
        if (!StringUtils.isEmpty(goodsId)){
            logService.save(new Log(Log.DELETE_ACTION,"删除商品库存:"+goodsId));

            goodsDao.deleteStock(goodsId);
        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    //查询库存报警商品信息
    @Override
    public Map<String, Object> getListAlarm() {
        Map<String,Object> map = new HashMap<>();

        List<Goods> list = goodsDao.getListAlarm();

        logService.save(new Log(Log.SELECT_ACTION,"查询库存量小于库存下限的商品信息"));

        map.put("rows", list);

        return map;

    }

}
